/*
 * @Author: your name
 * @Date: 2021-04-11 11:52:58
 * @LastEditTime: 2021-05-11 17:03:42
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\recordMod\service.go
 */
package recordMod

import "chronic/common"

func addOneRecord(record *recordDto) (*Record, error) {
	db := common.UseDb()
	recd := Record{Date: record.Date, Hospital: record.Hospital, PatientID: record.PatientID, PatientName: record.PatientName, PatientGender: record.PatientGender, PatientAge: record.PatientAge, PatientPhone: record.PatientPhone, Description: record.Description, DiseaseHistory: record.DiseaseHistory, Result: record.Result, Prescription: record.Prescription, Remark: record.Remark, DoctorName: record.DoctorName, DoctorID: record.DoctorID}
	err := db.Save(&recd).Error
	return &recd, err
}

func findRecordsByPatientId(patientId uint) ([]*Record, error) {
	db := common.UseDb()
	var records []*Record
	err := db.Where("patient_id = ?", patientId).Order("created_at desc").Find(&records).Error
	return records, err
}

func findAllRecords() ([]*Record, error) {
	db := common.UseDb()
	var records []*Record
	err := db.Order("created_at desc").Order("created_at desc").Find(&records).Error
	return records, err
}

func findRecordsByUserId(userId uint) ([]*Record, error) {
	db := common.UseDb()
	var records []*Record
	err := db.Where("patient_id=?", userId).Or("doctor_id=?", userId).Find(&records).Error
	return records, err
}

func findRecordsByCondition(condition string) ([]*Record, error) {
	db := common.UseDb()
	var records []*Record
	err := db.Where("hospital=?", condition).Or("doctor_name=?", condition).Or("patient_name=?", condition).Or("patient_phone=?", condition).Order("updated_at desc").Find(&records).Error
	return records, err
}

func deleteRecordsById(id uint) error {
	db := common.UseDb()
	err := db.Where("id = ?", id).Delete(&Record{}).Error
	return err
}
